package com.sphd.Restaurant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.base.BaseActivity;
import com.sphd.Restaurant.infos.AddressInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.AddressesP;
import com.sphd.Restaurant.utils.AppManager;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.AddressesView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressActivity extends BaseActivity implements AddressesView {
    Intent intent;
    Bundle bundle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.btn_new)
    TextView btnNew;
    private AddressesP addressesP;
    List<AddressInfo> addressInfos;
    BaseQuickAdapter<AddressInfo> baseQuickAdapter;
    int curpage = 1;
    int SwfAddList = 1;
    boolean isSelectAddr=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        baseQuickAdapter.setOnLoadMoreListener(() -> {
            curpage++;
            addData();
        });
        toolbar.setNavigationOnClickListener(view -> AppManager.getAppManager().finishActivity(AddressActivity.class));
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        if (bundle.getString(Constants.TAG).equals(AffirmOrderActivity.class.getName())) {
            setToolBar(toolbar, "选择收餐地址", true);
            isSelectAddr=true;
        } else {
            setToolBar(toolbar, "管理收餐地址", true);
            isSelectAddr=false;
        }
        addressInfos = new ArrayList<>();
        addressesP = new AddressesP(this);
        initAdapter();

    }

    private void initAdapter() {
        setRecy(myRecy);
        baseQuickAdapter = new BaseQuickAdapter<AddressInfo>(R.layout.address_item, addressInfos) {
            @Override
            protected void convert(BaseViewHolder helper, AddressInfo item) {
                helper.setText(R.id.tv_address_name, item.getUser_name())
                        .setText(R.id.tv_address_phone, item.getUser_phone())
                        .setText(R.id.tv_address_address, item.getAddr()).addOnClickListener(R.id.tv_alter)
                        .setVisible(R.id.tv_isdefault, helper.getLayoutPosition() == 0 ? true : false);
            }
        };
        baseQuickAdapter.openLoadAnimation();
        baseQuickAdapter.openLoadMore(10);
        myRecy.setAdapter(baseQuickAdapter);
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (isSelectAddr){
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constants.ADDRESSES, addressInfos.get(position));
                    Intent intent = new Intent();
                    intent.putExtras(bundle);
                    setResult(2, intent);
                    AppManager.getAppManager().finishActivity(AddressActivity.class);
                }
            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);

                bundle.putSerializable(Constants.ADDRESSES, addressInfos.get(position));
                bundle.putInt(Constants.TYPE, 0);
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(context, NewAddressActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        addData();
    }

    private void addData() {
        addressesP.addData(Model.HTTPURL + Model.ADDRESS + "userid=" + getInt(Constants.USER_ID) + "&curpage=" + curpage + "&pageno=10");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void showdialog() {

    }

    @Override
    public void dissmissdialog() {

    }

    @Override
    public void sendAddressList(List<AddressInfo> addressInfoList) {
        addressInfos.addAll(addressInfoList);
        if (addressInfoList.size() != 10) {
            baseQuickAdapter.loadComplete();
            addFootView(baseQuickAdapter);
        }
        baseQuickAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == SwfAddList) {
            curpage = 1;
            addressInfos.clear();
            baseQuickAdapter.removeAllFooterView();
            addData();
        }
    }

    @OnClick(R.id.btn_new)
    public void onClick() {
        bundle.putInt(Constants.TYPE, 1);
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setClass(context, NewAddressActivity.class);
        startActivityForResult(intent, 1);
    }
}
